'''
学生信息管理系统
'''
import time


# 注册
def reg():
    print('------------注册--------------')
    username = input('username:')
    password = input('password:')
    new_user = []
    if username and password:
        for user in users:
            if username == user[0]:
                print('账号已存在')
                return reg()
        else:
            new_user.append(username)
            new_user.append(password)
            users.append(new_user)
            print('注册成功，返回主页')
            time.sleep(0.5)
            return welcome()
    else:
        print('用户名和密码不能为空')
        return welcome()


# 登录
def login():
    print('-------------登录-------------------')
    while 1:
        username = input('username:')
        password = input('password:')
        for user in users:
            if username == user[0] and password == user[1]:
                print('登录成功！')
                print('正在加载主页面...')
                time.sleep(1)
                return choose()
        else:
            print('账号或密码输入有误，请重新登录！')
            return welcome()


# 功能选择
def choose():
    print('请输入功能编号：')
    print('1添加信息 2删除信息 3查找信息 4退出')
    while 1:
        num1 = input('请输入功能编号：')
        if num1 == '1':
            return _add()
        elif num1 == '2':
            return _delete()
        elif num1 == '3':
            return search()
        elif num1 == '4':
            return _exit()
        else:
            print('输入编号有误，请重新输入！')


# 添加
def _add():
    print('-------------添加**信息-------------')
    while 1:
        sid = input('学号（非空）：')
        name = input('名称（非空）：')
        gender = input('性别(可为空)：')
        clazz = input('班级(可为空)：')
        tel = input('电话(可为空)：')
        new_inf = {}
        if sid and name:
            new_inf['sid'] = sid
            new_inf['name'] = name
            new_inf['gender'] = gender
            new_inf['clazz'] = clazz
            new_inf['tel'] = tel
            for inf in all_inf:
                if sid == inf['sid']:
                    print('学号已存在，添加失败')
                    continue
            else:
                all_inf.append(new_inf)
                print('添加成功')
                print('1继续添加 2查询 3返回')
                while 1:
                    num3 = input('请输入编号：')
                    if num3 == '1':
                        return _add()
                    elif num3 == '2':
                        return search()
                    elif num3 == '3':
                        return choose()
                    else:
                        print('输入编号错误，请重新输入：')
        else:
            print('学号和名称不能为空，请重新添加')


# 删除
def _delete():
    print('---------------删除页面---------------------')
    print('1一键删库跑路 2按学号删除 3返回')
    while 1:
        num4 = input('请输入编号：')
        if num4 == '1':
            print('是否确定删库跑路，慎重！')
            answer = input('y/n:')
            if answer == 'y':
                time.sleep(1)
                all_inf.clear()
                print('兄弟，赶紧跑.......')
                exit()
            else:
                return _delete()
        elif num4 == '2':
            sid = input('删除学号：')
            for inf in all_inf:
                if inf['sid'] == sid:
                    all_inf.remove(inf)
                    print('删除成功')
                    print('1查询 2返回')
                    while 1:
                        num5 = input('请输入编号：')
                        if num5 == '2':
                            return _delete()
                        elif num5 == '1':
                            return search()
                        else:
                            print('输入编号错误，请重新输入：')
            else:
                print('无此学号')
                return _delete()

    pass


# 查询
def search():
    print('----------------查找----------------')
    print('1所有信息 2按照条件查找 3返回')
    while 1:
        num2 = input('请输入编号：')
        if num2 == '1':
            print('-----所有**信息如下-------')
            for inf in all_inf:
                print(inf)
            return search()
        elif num2 == '2':
            search_msg = input('请输入学号或者名称')
            for inf in all_inf:
                if search_msg == inf['sid'] or search_msg == inf['name']:
                    print(inf)
                    return search()
            else:
                print('无{}相关的信息'.format(search_msg))
        elif num2 == '3':
            return choose()
        else:
            print('输入编码有误，请重新输入：')


# 退出
def _exit():
    print('系统退出中...')
    time.sleep(0.5)
    print('退出成功！')
    exit()


def welcome():
    print('1登录 2注册 3退出')
    while 1:
        num = input('请输入编号：')
        if num == '1':
            login()
        elif num == '2':
            reg()
        elif num == '3':
            _exit()
        else:
            print('编码输入错误，请重新输入')


print('-------------欢迎进入**管理系统-----------------')
# 初始化信息
# 账号列表
users = [
    ['1', '1']
]
# **信息列表
all_inf = [
    {'sid': '1', 'name': '1', 'gender': '1', 'clazz': '1', 'tel': '1'}
]
# 主页
welcome()
